﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Linq;
using System.Data.Linq;
using WX.WMS.DataModel;
using WX.WMS.Common;
using WX.WMS.BLL;

public partial class Stock_DetopManager : PageBase
{
    private string action
    {
        get
        {
            if (ViewState["action"] == null) return "";
            else return ViewState["action"].ToString();
        }
        set { ViewState["action"] = value; }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            init();
        }

    }
    protected void init()
    {
        action = Request.QueryString["action"];
        DataBandDrowDownList();
        if (action != null)
        {
            if (action.ToString().Equals("edit"))
            {
                string ID = Request.QueryString["ID"].ToString();
                StoreHouseBLL BllStoreHouse = new StoreHouseBLL();
                T_STOCK_AREA StockArea = BllStoreHouse.GetStockAreaByID(ID);
                AreaName.Text = StockArea.AREA_NAME;
                ListItem Item = StoreHouse.Items.FindByValue(StockArea.T_STOCK.STOCK_ID.ToString());
                StoreHouse.SelectedIndex = StoreHouse.Items.IndexOf(Item);
                updateButton.Text = "修改";

            }
            else if (action.ToString().Equals("del"))
            {
                string ID = Request.QueryString["ID"].ToString();
                StoreHouseBLL BllStoreHouse = new StoreHouseBLL();
                BllStoreHouse.DeleteStockArea(ID);
            }
        }
        else
        {
            
            updateButton.Text = "添加";
        }

        
        DataBand();



    }
    /// <summary>
    /// 
    /// </summary>

    protected void DataBandDrowDownList()
    {
        StoreHouseBLL BllStoreHouse = new StoreHouseBLL();
        StoreHouse.DataSource = BllStoreHouse.GetStockList();
        StoreHouse.DataBind();
    }


    protected void DataBand()
    {
        StoreHouseBLL BllStoreHouse = new StoreHouseBLL();
        SearchResult.DataSource = BllStoreHouse.GetStockAreaList();
        SearchResult.DataBind();
    }

    protected void updateButton_Click(object sender, EventArgs e)
    {
        if (action == "edit")
        {
            string ID = Request.QueryString["ID"].ToString();
            StoreHouseBLL BllStoreHouse = new StoreHouseBLL();
            T_STOCK_AREA StockArea = BllStoreHouse.GetStockAreaByID(ID);
            StockArea.AREA_NAME = AreaName.Text.Trim();
            StockArea.STOCK_ID = new Guid(StoreHouse.SelectedValue);
            BllStoreHouse.UpdateStockArea(StockArea);
            action = null;
            updateButton.Text = "添加";
            AreaName.Text = "";
            StoreHouse.SelectedIndex = 0;
        }
        else
        {
            StoreHouseBLL BllStoreHouse = new StoreHouseBLL();
            T_STOCK_AREA StockArea = new T_STOCK_AREA();
            StockArea.AREA_NAME = AreaName.Text.Trim();
            StockArea.STOCK_ID=new Guid(StoreHouse.SelectedValue);
            BllStoreHouse.AddStockArea(StockArea);

        }
        DataBand();
    }
    protected void SearchResult_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        SearchResult.PageIndex = e.NewPageIndex;
        action = null;
        AreaName.Text = "";
        init();
    }
       

}
